package com.sinux.generality.basesupport.utils.kafka.test;

import com.sinux.generality.basesupport.utils.kafka.consumer.ConsumerFactory;
import com.sinux.generality.basesupport.utils.kafka.consumer.KafkaConsumerInterface;
import com.sinux.generality.basesupport.utils.kafka.serialization.TransferObject;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.*;


public class ConsumerDemo {
    public static void main(String[] args) {
//        Properties p = new Properties();
//        p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "121.5.143.221:9092");
//        p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
//        p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
//        p.put(ConsumerConfig.GROUP_ID_CONFIG, "dtest");
//        p.put("auto.offset.reset","earliest");
//        p.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
//        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(p);
//        consumer.subscribe(Arrays.asList("test", "bar"));
//        while (true) {
//            ConsumerRecords<String, String> records = consumer.poll(1000);
//            for (ConsumerRecord<String, String> record : records) {
//                System.out.println(record);
//            }
//        }
        //ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,
//        List<String> list = new ArrayList<>();
//        list.add("121.5.143.221:9092");
//        KafkaConsumerInterface aDefault = ConsumerFactory.buildConsumerClassByBoostrapServers("default", list,"duanjt_test");

        Map<String,Object> configMap = new HashMap<>();
        configMap.put("bootstrap.servers", "121.5.143.221:9092");
        configMap.put("group.id", "dtest");
        configMap.put("auto.offset.reset","earliest");
        configMap.put("enable.auto.commit", false);
        KafkaConsumerInterface aDefault = ConsumerFactory.buildConsumerClassByConfigMap("default",configMap);

        while (true) {
            ConsumerRecords<String, TransferObject> records = aDefault.poll(Collections.singletonList("test"),100);
            for (ConsumerRecord<String, TransferObject> record : records) {
                System.out.println("=====================>"+record);
            }
            aDefault.commitSync(Collections.singletonList("test"));
        }
    }
}